package com.etc.controller;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.etc.entity.Goodstype;
import com.etc.entity.Order;
import com.etc.service.GoodsTypeService;
import com.etc.service.OrdersService;
import com.etc.tools.AjaxOperateResponse;
import com.etc.tools.ResponseData;
import com.fasterxml.jackson.annotation.JsonAlias;
import com.fasterxml.jackson.annotation.JsonFormat;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@RestController
@RequestMapping("api")
@Api(value = "订单接口", description = "处理订单操作的后端接口")
public class OrderController {

	@Autowired
	private OrdersService orderService;

	@GetMapping("order")
	@ApiOperation(value = "查询类型", httpMethod = "GET", notes = "分页加模糊查询的方法", response = ResponseData.class)
	public ResponseData<Order> listOrder(
			@ApiParam(name = "keywords", required = true, defaultValue = "", value = "查询关键字") @RequestParam(name = "keywords", required = true, defaultValue = "") String keywords,
			@ApiParam(name = "page", required = true, defaultValue = "1", value = "页码") @RequestParam(name = "page", required = true, defaultValue = "1") Integer page,
			@ApiParam(name = "limit", required = true, defaultValue = "10", value = "每页记录数") @RequestParam(name = "limit", required = true, defaultValue = "10") Integer limit) {

		ResponseData<Order> rd = orderService.getByLike(keywords, page, limit);
		return rd;
	}

	@PostMapping("order")
	@ApiOperation(value = "增加订单类型", httpMethod = "POST", notes = "增加订单", response = AjaxOperateResponse.class)
	public AjaxOperateResponse addOrder(@RequestBody Order order) {
		return orderService.addOrder(order);
	}

	@PutMapping("order") // 主动修改订单不多，主要订单状态修改
	@ApiOperation(value = "修改订单", httpMethod = "PUT", notes = "修改订单", response = AjaxOperateResponse.class)
	public AjaxOperateResponse updateOrder(@RequestBody Order order) {
		return orderService.updateOrder(order);
	}

	@DeleteMapping("order/{orderid}")
	@ApiOperation(value = "删除订单", httpMethod = "DELETE", notes = "删除订单", response = AjaxOperateResponse.class)
	public AjaxOperateResponse delOrder(
			@ApiParam(name = "orderid", required = true, value = "删除的id") @PathVariable("orderid") String orderid) {
		return orderService.delOrder(orderid);
	}

	@GetMapping("order/{orderid}")
	@ApiOperation(value = "根据id查询", httpMethod = "GET", notes = "查询订单", response = Order.class)
	public Order getOrderById(
			@ApiParam(name = "orderid", required = true, value = "根据id查询，返回对象信息") @PathVariable("orderid") String orderid) {

		return orderService.getOrderById(orderid);

	}
	
	@GetMapping("orderdate")
	@ApiOperation(value = "根据orderdate查询", httpMethod = "GET", notes = "查询订单", response = Order.class)
	public Order getOrderByDate(
			@ApiParam(name = "orderdate", required = true, value = "根据orderdate查询，返回对象信息") @RequestParam("orderdate") Date orderdate) {

		System.out.println(orderdate);
		return orderService.getOrderById("1001");

	}
}
